'use strict';

const AsyncLock = require('async-lock');
const { bingTranslator } = require('translators')

var lock = new AsyncLock();
var translateId = 0;
const translate = (str) => {
    return new Promise(resolve => {
        lock.acquire('translate', function () {
            translateId += 1;
            console.log(`t${translateId}`)
            return bingTranslator(str, 'en', 'zh');
        }).then((value) => {
            resolve(value);
        })
    });
}

class TranslatorPipeline {

    async process_item(item, spider, request) {
        const { log } = spider;
        const { title, text, tags } = item;

        let ctitle = '', ctext = '', ctags = []
        if (title) {
            ctitle = await translate(title)
        }
        if (text) {
            let textArr = text.split('\n')
            let cArr = []
            for (let line of textArr) {
                if (line.length > 0) {
                    cArr.push(await translate(line))
                } else {
                    cArr.push('')
                }
            }
            ctext = cArr.join('\n')
        }
        if (tags && tags.length > 0) {
            for (let tag of tags) {
                ctags.push(await translate(tag))
            }
        }
        return Object.assign(item, {
            ctitle,
            ctext,
            ctags,
        });
    }
}

module.exports = TranslatorPipeline;